//	Page object contains all pre-functions and callbacks
Page = new Object;
Page.callback = function() { }
Page.init = function() {
	$('body').bind('blur focus resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error ready', function(event) {
		event.preventDefault();
		event.stopImmediatePropagation();
		event.stopPropagation();
		Page.respond(event);
	});
}
Page.respond = function() { }

//	Architect handles all server transactions
Architect = new Object;
Architect.hideError = function() { $('#errorDiv').hide('slide', {direction: 'down'}, 'fast'); };
Architect.hideLoading = function() { $('#loadingDiv').hide('slide', {direction: 'up'}, 'fast'); };
Architect.hideMsg = function() { $('#msgDiv').hide('slide', {direction: 'down'}, 'fast'); };
Architect.init = function() {
	Architect.transaction();
}
Architect.showError = function(txt) { $('#errorDiv').html(txt).show('slide', {direction: 'up'}, 'fast'); }
Architect.showLoading = function() { $('#loadingDiv').show('slide', {direction: 'down'}, 'fast'); }
Architect.showMsg = function(txt) { $('#msgDiv').html(txt).show('slide', {direction: 'up'}, 'fast'); }
Architect.transaction = function(state) {
	/*	transaction sends the action name and args object (as determined by Page)
		the return value is handled by Proxy callbackHandler
		state = {view:'viewName', state:'viewContext', action:'userAction', args:{}}
	*/
	Architect.showLoading();
	if (state) {
		Proxy.update(state['view'], state['context'], state['action'], state['args']);
	} else {
		Proxy.update();
	}	
}
Architect.uploadFile = function (fileElement, callback) {
	$('#loadingDiv')
		.ajaxStart(function() { $(this).show('slide', {direction: 'up'}, 'fast'); })
		.ajaxStop(function() { $(this).hide('slide', {direction: 'down'}, 'fast'); });
		
	$.ajaxFileUpload({
		url				:	'./ajax/fileUpload.cfm?id='+fileElement,
		secureuri		:	false,
		fileElementId	:	fileElement,
		dataType		:	'json',
		success			:	function (data, status) {
			if(typeof(data.error) != 'undefined') {
				if(data.error != '') {
					Architect.showError(data.error);
				} else {
					Architect.showMsg(data.msg);
					Page.callback;
				}
			}
		},
		error	:	function (data, status, e) {
			alert(e);
		}
	});
}
// end window.Architect construct

//	Proxy (cfajaxproxy obj) manages CFC calls
var Proxy = new CFproxy();
Proxy.init();
Proxy.setHTTPMethod('POST');
Proxy.setCallbackHandler(function(reply) {
	/*
		the returned markup, and the container in which it goes, is determined by the server
			if no container is specified the entire mainContent div contents are replaced
	*/
	Architect.hideError();
	Architect.hideMsg();
	
	if (reply) {
		if (reply["markup"].length) {
			if (reply["container"].length) {
				$('#'+container).html(reply["markup"]);
			} else {
				$('#mainContent').html(reply["markup"]);
			}
		}
		
		if (reply["msg"].length) Architect.showMsg(reply["msg"]);
		
		if (reply["error"].length) Architect.showError(reply["error"]);
/*
		if (reply["args"].length) {
			Page.callback(reply["args"]);
		} else {
			Page.callback();
		}
*/
	}
	
	$.getScript('/ajax/styles.cfm');
	$.getScript('/ajax/script.cfm');
	Page.init();
	Architect.hideLoading();	// loading div is shown before the transcation starts
});
Proxy.setErrorHandler(function(statusCode, statusMsg) { Architect.showError('Error: ' + statusCode + ', ' + statusMsg); })
